package applet;

import java.util.*;

import processing.core.PApplet;

public class BarChart {
	float xPos, yPos;
	float width, height;
	ArrayList<ChartElement> barElements;
	int elementCount;
	float sum;
	float max;
	int[] color;

	BarChart(float x, float y, float w, float h, ArrayList<ChartElement> be) {
		xPos = x;
		yPos = y;
		width = w;
		height = h;
		barElements = be;
		elementCount = barElements.size();
		sum = 0;
		max = 0;
		for(ChartElement p: barElements) {
			sum+=p.number;
			if(p.number>max) {
				max = p.number;
			}
		}
		PApplet.println("Sum is: "+sum+", max is: "+max);
		color = new int[10];
		color[0] = GLOBAL.processing.color(0x56,0x79,0xC1);
		color[1] = GLOBAL.processing.color(0xD1,0x16,0x49);
		color[2] = GLOBAL.processing.color(0x79,0x79,0x79);//#79C156;
		color[3] = GLOBAL.processing.color(0x59,0xC6,0x71);
		color[4] = GLOBAL.processing.color(0xBF,0xBF,0x5D);
		color[5] = GLOBAL.processing.color(0x71,0x59,0xC6);
		color[6] = GLOBAL.processing.color(0xC6,0x71,0x59);
		color[7] = GLOBAL.processing.color(0x79,0xC1,0x56);
		color[8] = GLOBAL.processing.color(0xE1,0x39,0xA1);
		color[9] = GLOBAL.processing.color(0xBA,0xBA,0xBA);
		
	}
	
	void draw() {
		GLOBAL.processing.stroke(192);
		GLOBAL.processing.line(xPos+0.1f*width, yPos+0.9f*height, xPos+width, yPos+0.9f*height);
		GLOBAL.processing.line(xPos+0.1f*width, yPos/*+0.0f*height*/, xPos+0.1f*width, yPos+0.9f*height);
		GLOBAL.processing.textFont(GLOBAL.fontTimes12,12);
		GLOBAL.processing.textAlign(PApplet.RIGHT,PApplet.CENTER);
		GLOBAL.processing.fill(192);
		for(int i = 1; i <= 4; i++) {
			float y = PApplet.map((int)(max/4*i), 0, max, yPos+0.9f*height, yPos+0.05f*height);
			GLOBAL.processing.text((int)(max/4*i),xPos+0.1f*width,y);
		}
		GLOBAL.processing.textAlign(PApplet.RIGHT,PApplet.CENTER);
		GLOBAL.processing.pushMatrix();
		GLOBAL.processing.translate(xPos+0.1f*width, yPos+0.92f*height);
		GLOBAL.processing.rotate(-PApplet.HALF_PI);

		for(int i = 0; i<elementCount; i++) {
			float x = PApplet.map(i, 0, elementCount, 0, 0.9f*width);
//			float x = PApplet.map(i, 0, elementCount, xPos+0.1f*width, xPos+width);
//			GLOBAL.processing.pushMatrix();
//			GLOBAL.processing.translate(x, yPos+0.92f*height);
//			GLOBAL.processing.rotate(-PApplet.HALF_PI);
			GLOBAL.processing.text(barElements.get(i).label,0,x);
		}
//		GLOBAL.processing.pushMatrix();
//		GLOBAL.processing.translate(xPos+width,yPos+0.92f*height);
//		GLOBAL.processing.rotate(-PApplet.HALF_PI);
//		GLOBAL.processing.text("+",0,0);
		GLOBAL.processing.text("+",0,0.9f*width);
		GLOBAL.processing.popMatrix();
		GLOBAL.processing.noStroke();
		for(int i = 0; i<elementCount; i++) {
			float y = PApplet.map(barElements.get(i).number, 0, max, 0, 0.85f*height);//yPos+0.9f*height, yPos+0.05f*height);
			float x = PApplet.map(i+0.5f, 0, elementCount, xPos+0.1f*width, xPos+width);
			GLOBAL.processing.fill(color[(i+5)%10]);
			GLOBAL.processing.rect(x-width/(elementCount+5)/2, yPos+0.9f*height, width/(elementCount+5), -y);
		}

	}
	void setPosition(float x, float y) {
		xPos = x;
		yPos = y;
	}
	void setSize(float w, float h) {
		width = w;
		height = h;
	}
	void setData(ArrayList<ChartElement> be) {
		barElements = be;
		elementCount = barElements.size();
		sum = 0;
		max = 0;
		for(ChartElement p: barElements) {
			sum+=p.number;
			if(p.number>max) {
				max = p.number;
			}
		}
	}
}
